package com.shanghaionstar.remote;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

import com.shanghaionstar.R;
import com.shanghaionstar.utils.AlertDialogUtil;

/**
 * Created by 6335yz on 2016/8/26.
 */
public class RemoteFailDialog extends Dialog {
    private String input;

    public RemoteFailDialog(Context context, String input) {
        super(context);
        this.input = input;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.remote_fail_dialog);
        final String phone = getContext().getString(R.string.connect_phone);
        TextView textView = (TextView) findViewById(R.id.content);
        SpannableString ss = new SpannableString(input);
        ss.setSpan(new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                AlertDialogUtil.showAlertDialog(getContext(), getContext().getString(R.string.dialog_warning),phone, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phone));
                        getContext().startActivity(intent);
                    }
                }, true);
                dismiss();
            }
        }, input.length() - phone.length(), input.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        ss.setSpan(new ForegroundColorSpan(getContext().getResources().getColor(R.color.color_0a4ba6)), input.length() - phone.length(), input.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        textView.setText(ss);
        textView.setMovementMethod(LinkMovementMethod.getInstance());
        findViewById(R.id.i_know).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }
}
